﻿using System;
using System.Diagnostics;
using Microsoft.Xna.Framework.Input;
using Soundteller.AudioVisual.Model;
using Soundteller.AudioVisual.Services.Interfaces;
using Soundteller.Phone.Facade.Interfaces;
using Soundteller.Phone.Facade.Mappers;
using Microsoft.Xna.Framework.Input.Touch;
using Soundteller.Bus.Services.Interfaces;

namespace Soundteller.Phone.Facade
{
    public class AudioVisualFacade : IAudioVisualFacade
    {
        private readonly ISoundtellerInitializer _SoundtellerInitializer;
        private readonly ISoundtellerDrawer _SoundtellerDrawer;
        private readonly IPadToucher _padToucher;
        private readonly IPadStateUpdater _padStateUpdater;
        private readonly ISubscriber _subscriber;
        private readonly IPadStateResetter _padStateResetter;

        public AudioVisualFacade(ISoundtellerInitializer SoundtellerInitializer, ISoundtellerDrawer SoundtellerDrawer, IPadToucher padToucher,
            IPadStateUpdater padStateUpdater, ISubscriber subscriber, IPadStateResetter padStateResetter)
        {
            if (SoundtellerInitializer == null) throw new ArgumentNullException("SoundtellerInitializer");
            if (SoundtellerDrawer == null) throw new ArgumentNullException("SoundtellerDrawer");
            if (padToucher == null) throw new ArgumentNullException("padToucher");
            if (padStateUpdater == null) throw new ArgumentNullException("padStateUpdater");
            if (subscriber == null) throw new ArgumentNullException("subscriber");
            if (padStateResetter == null) throw new ArgumentNullException("padStateResetter");

            _SoundtellerInitializer = SoundtellerInitializer;
            _SoundtellerDrawer = SoundtellerDrawer;
            _padToucher = padToucher;
            _padStateUpdater = padStateUpdater;
            _subscriber = subscriber;
            _padStateResetter = padStateResetter;
        }

        public void InitializeSoundteller(int canvasWidth, int canvasHeight)
        {
            Debug.WriteLine("{0}.InitializeSoundteller()", GetType().Namespace, null);

            _SoundtellerInitializer.Initialize(new InitializeSoundtellerRequest { CanvasWidth = canvasWidth, CanvasHeight = canvasHeight });
        }

        public void UpdateSoundteller()
        {
            Debug.WriteLine("{0}.UpdateSoundteller()", GetType().Namespace, null);

            _SoundtellerDrawer.Draw();
        }

        public void TouchPad(TouchLocation touchLocation)
        {
            Debug.WriteLine("{0}.TouchPad( {1} )", GetType().Namespace, touchLocation);

            var arguments = TouchLocationToPadTouchArgumentsMapper.Map(touchLocation);

            _padToucher.Touch(arguments);
        }

        public void SetPadInPlayingState(string soundName)
        {
            Debug.WriteLine("{0}.UpdatePadState( {1} )", GetType().Namespace, soundName);

            _padStateUpdater.SetInPlayingState(soundName);
        }

        public void SubscribePadStateUpdater()
        {
            Debug.WriteLine("{0}.SubscribePadStateUpdater()", GetType().Namespace, null);

            _subscriber.Subscribe(_padStateUpdater);
        }

        public void SubscribePadStateResetter()
        {
            Debug.WriteLine("{0}.SubscribeSoundtellerResetter()", GetType().Namespace, null);

            _subscriber.Subscribe(_padStateResetter);
        }
    }
}